package blog.util;

public class RegUtils {
	
	public static char[] reg = { 0x42, 0x6c, 0x6f, 0x67, 0x4c, 0x69, 
		0x76, 0x65, 0x46, 0x6f, 0x72, 0x45, 0x76, 0x65, 0x72 };
	public static String format( String mobile, String registCode )
	{
		if ( mobile==null || registCode==null || mobile.length()==0 || registCode.length()==0)
		{
			return "";
		}
		String tmp = "";
		long hash = abs(string2long(mobile)) + abs(string2long(registCode));
		tmp += hash;

		StringBuffer sb = new StringBuffer();
		boolean isDouble = false;
		int i, n;
		for( i=0, n=tmp.length(); i<n; i++ )
		{
			char c = tmp.charAt(i);
			if ( isDouble )
			{
				sb.append(increase(c));
			}
			else
			{
				sb.append(decrease(c));
			}
			isDouble = !isDouble;
		}
		int offset = (mobile.hashCode()*registCode.hashCode()) % reg.length;
		offset = abs( offset );
		for( ; i<reg.length; i++ )
		{
			if ( isDouble )
			{
				sb.append(increase(reg[(i*2 -1 + offset)%reg.length]));
			}
			else
			{
				sb.append(decrease(reg[(i*2 +1 + offset)%reg.length]));
			}
			isDouble = !isDouble;
		}
		return sb.toString();
	}
	
	private static char increase( char c ){if( c==0x39 )return 0x41;else if( c==0x5A )return 0x61;else if( c==0x7A )return 0x30;else if( c=='-' || c=='+' )return c;else return (char)(c+1);}
	
	private static char decrease( char c ){if( c==0x30 )return 0x7A;else if ( c==0x41 )return 0x39;else if ( c==0x61 )return 0x5A;else if ( c=='-' || c=='+' )return c;else return (char)(c-1);}
	
	private static int abs(int x){if ( x<0){return -x;}else{return x;}}
	
	private static long abs(long x){if ( x<0){return -x;}else{return x;}}
	
	private static long string2long(String str){char[] chars = str.toCharArray();long returnValue = 1;for( int i=0; i<chars.length; i++ ){returnValue = returnValue*7 + ((int)chars[i]) % 7;}return returnValue;}
	
	public static void main(String[] args )
	{
		if ( args.length<2 )
		{
			System.out.println("Tips:\nYou should input two arguments:\n  [1]:mobile no\n  [2]:regist code\nThanks.");
		}
		else
		{
			System.out.println(format(args[0], args[1]));
		}
	}
}
